package cn.com.zpark.servlet_context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @PackageName: cn.com.zpark.servlet_context
 * @ClassName: ServletContextDemo06
 * @Description:
 * @author: RZS
 * @date: 2021/11/14  14:41
 */
@WebServlet("/scd6.do")
public class ServletContextDemo06 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取context对象
        ServletContext context = getServletContext();
        // 获取流对象
        InputStream stream = context.getResourceAsStream("db.properties");

        // 创建properties工具类
        Properties p = new Properties();
        // 加载流对象
        p.load(stream);

        String username = p.getProperty("username");
        String password = p.getProperty("password");
        String url = p.getProperty("url");

        System.out.println(username);
        System.out.println(password);
        System.out.println(url);
    }
}
